﻿using System;
using System.Linq;
using WebExam.DataEntities;
using WebExam.DataProvider;

namespace WebExam.SQLDataProvider
{
    public class XSRepository : BaseRepository<XS>, IXSRepository
    {
        /// <summary>
        /// 根据学号获取学生信息
        /// </summary>
        /// <param name="xsId">学号</param>
        public XS Get(string xsId)
        {
            using (ThisDb)
            {
                return ThisTable.FirstOrDefault(p => string.Equals(p.XSId, xsId, StringComparison.InvariantCultureIgnoreCase));
            }
        }

        /// <summary>
        /// 根据证件号获取学生信息
        /// </summary>
        /// <param name="idCard">证件号</param>
        public XS GetByIdcard(string idCard)
        {
            using (ThisDb)
            {
                return ThisTable.FirstOrDefault(p => string.Equals(p.XSIdcard, idCard, StringComparison.InvariantCultureIgnoreCase));
            }
        }

        /// <summary>
        /// 根据班级Id获取学生信息
        /// </summary>
        /// <param name="bjId">班级Id</param>
        /// <param name="start">开始位置</param>
        /// <param name="takeSize">获取记录条数</param>
        public IQueryable<XS> GetAll(int bjId, int start, int takeSize)
        {
            using (ThisDb)
            {
                return ThisTable.Where(p => p.BJNum == bjId).OrderBy(p => p.XSId).Skip(start).Take(takeSize);
            }
        }

        /// <summary>
        /// 添加学生信息
        /// </summary>
        /// <param name="xs">学生对象</param>
        public XS Add(XS xs)
        {
            using (ThisDb)
            {
                try
                {
                    ThisTable.InsertOnSubmit(xs);
                    ThisDb.SubmitChanges();
                    return xs;
                }
                catch
                {
                    return xs;
                }
            }
        }

        /// <summary>
        /// 更新学生信息
        /// </summary>
        /// <param name="xsId">学号</param>
        /// <param name="update">包含更新操作的方法</param>
        public XS Update(string xsId, Action<XS> update)
        {
            using (ThisDb)
            {

                var obj = ThisTable.FirstOrDefault(p => string.Equals(p.XSId, xsId, StringComparison.InvariantCultureIgnoreCase));

                if (obj == null)
                {
                    return null;
                }

                update(obj);

                try
                {
                    ThisDb.SubmitChanges();
                    return obj;
                }
                catch
                {
                    return null;
                }
            }
        }

        /// <summary>
        /// 删除学生
        /// </summary>
        /// <param name="xsId">学号</param>
        public bool Delete(string xsId)
        {
            using (ThisDb)
            {
                var obj = ThisTable.FirstOrDefault(p => string.Equals(p.XSId, xsId, StringComparison.InvariantCultureIgnoreCase));

                if (obj == null)
                {
                    return true;
                }

                try
                {
                    ThisTable.DeleteOnSubmit(obj);
                    ThisDb.SubmitChanges();
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }

        /// <summary>
        /// 根据班级Id统计班级个数
        /// </summary>
        /// <param name="bjId">班级Id</param>
        public int Count(int bjId)
        {
            using(ThisDb)
            {
                return ThisTable.Count(p => p.BJNum == bjId);
            }
        }
    }
}